﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using TMD.GT.Site.Controladora;
using TMD.GT.Site.Util;
using TMD.GT.Entidades;
using System.Text;

namespace TMD.GT.Site.Vistas.AlineacionComponente
{
    public partial class Aprobar_Riesgos : System.Web.UI.Page
    {

        decimal UmbarlVC = Convert.ToDecimal(System.Configuration.ConfigurationManager.AppSettings.Get("VC_UMBRAL"));
        //int icalculo = 0;
        decimal dcalculo = 0;

        List<Probabilidad> listaProbabilidad = ProbabilidadControladora.ListarProbabilidad();
        List<Impacto> listaImpacto = ImpactoControladora.ListarImpacto();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Componente componente = ComponenteControladora.ListarComponentes(new Componente { Codigo = Request.QueryString["ID_componente"].ToString(), Concepto = "", CodigoTipo = 0 }).FirstOrDefault();
                
                lblCodigo.Text = componente.Codigo;
                lblDescripcion.Text = componente.Nombre;
                lblEstado.Text = componente.EstadoComponente;
                lblPrioridad.Text = componente.Priorizacion.ToString();
                if (string.IsNullOrEmpty(componente.ObsAprobacion))
                {
                    txAObservacion.Text = "";
                }
                else
                {
                    txAObservacion.Text = componente.ObsAprobacion.ToString();
                }
                
                
                if (componente.EstadoComponente == "AC")
                {
                    optActivar.Checked = true;
                }
                if (componente.EstadoComponente == "IN")
                {
                    optNoActivar.Checked = true;
                }
                

                gvRiesgos.DataSource = RiesgoControladora.ListarRiesgos();
                gvRiesgos.DataBind();

                List<RiesgoComponente> lista = RiesgoComponenteControladora.ListarRiesgosComponente(componente);

                foreach (GridViewRow row in gvRiesgos.Rows)
                {
                    CheckBox check = row.FindControl("chkRiesgo") as CheckBox;
                    HiddenField hdd = row.FindControl("hddRiesgo") as HiddenField;
                    DropDownList ddlProb = row.FindControl("ddlProbabilidad") as DropDownList;
                    DropDownList ddlImpa = row.FindControl("ddlImpacto") as DropDownList;
   

                    ddlProb.DataSource = ProbabilidadControladora.ListarProbabilidad();
                    ddlProb.DataValueField = "ID_PROBABILIDAD";
                    ddlProb.DataTextField = "DESCRIPCION";
                    ddlProb.DataBind();

                    ddlImpa.DataSource = ImpactoControladora.ListarImpacto();
                    ddlImpa.DataValueField = "ID_IMPACTO";
                    ddlImpa.DataTextField = "DESCRIPCION";
                    ddlImpa.DataBind();

                    foreach (RiesgoComponente c in lista) 
                    {
                        if (c.CODIGO_RIESGO == Convert.ToInt32(hdd.Value))
                        {
                            check.Checked = true;
                            ddlProb.SelectedValue = c.PROBABILIDAD.ToString();
                            ddlImpa.SelectedValue = c.IMPACTO.ToString();

                            //icalculo += (Convert.ToInt32(ddlProb.SelectedValue) * Convert.ToInt32(ddlImpa.SelectedValue)); 
                            dcalculo += (listaProbabilidad.Find(p => p.ID_PROBABILIDAD == (Convert.ToInt32(ddlProb.SelectedValue)))).VALOR * (listaImpacto.Find(p => p.ID_IMPACTO== (Convert.ToInt32(ddlImpa.SelectedValue)))).VALOR;
                        }
                    }
                }




                txtValorBalanceo.Text = dcalculo.ToString();//icalculo.ToString();

                if (Convert.ToDecimal(txtValorBalanceo.Text) >= UmbarlVC)
                {
                    optActivar.Enabled = false;
                    optNoActivar.Enabled = false;
                }
                else
                {
                    optActivar.Enabled = true;
                    optNoActivar.Enabled = true;
                }




            }

        }

        protected void ddlImpactoIndexChange(object sender, EventArgs e)
        {

            foreach (GridViewRow row in gvRiesgos.Rows)
            {
                CheckBox check = row.FindControl("chkRiesgo") as CheckBox;
                DropDownList ddlProb= row.FindControl("ddlProbabilidad") as DropDownList;
                DropDownList ddlImpa= row.FindControl("ddlImpacto") as DropDownList;
                
                HiddenField hdd = row.FindControl("hddRiesgo") as HiddenField;

                if (check.Checked == true)
                {
                    //icalculo += (Convert.ToInt32(ddlProb.SelectedValue) * Convert.ToInt32(ddlImpa.SelectedValue));
                    dcalculo += (listaProbabilidad.Find(p => p.ID_PROBABILIDAD == (Convert.ToInt32(ddlProb.SelectedValue)))).VALOR * (listaImpacto.Find(p => p.ID_IMPACTO == (Convert.ToInt32(ddlImpa.SelectedValue)))).VALOR;
                }

            }

            txtValorBalanceo.Text = dcalculo.ToString();//icalculo.ToString();

            if (Convert.ToDecimal(txtValorBalanceo.Text)>= UmbarlVC)
            {
                optActivar.Enabled = false;
                optNoActivar.Enabled = false;
            }
            else
            {
                optActivar.Enabled = true;
                optNoActivar.Enabled = true;
            }

        }

        protected void ddlProbabilidadIndexChange(object sender, EventArgs e)
        {

            foreach (GridViewRow row in gvRiesgos.Rows)
            {
                CheckBox check = row.FindControl("chkRiesgo") as CheckBox;
                DropDownList ddlProb = row.FindControl("ddlProbabilidad") as DropDownList;
                DropDownList ddlImpa = row.FindControl("ddlImpacto") as DropDownList;

                HiddenField hdd = row.FindControl("hddRiesgo") as HiddenField;

                if (check.Checked == true)
                {
                    //icalculo += (Convert.ToInt32(ddlProb.SelectedValue) * Convert.ToInt32(ddlImpa.SelectedValue)); 
                    dcalculo += (listaProbabilidad.Find(p => p.ID_PROBABILIDAD == (Convert.ToInt32(ddlProb.SelectedValue)))).VALOR * (listaImpacto.Find(p => p.ID_IMPACTO == (Convert.ToInt32(ddlImpa.SelectedValue)))).VALOR;
                }
                else
                {
                    optActivar.Enabled = true;
                    optNoActivar.Enabled = true;
                }
            }

            txtValorBalanceo.Text = dcalculo.ToString();// icalculo.ToString();

            if (Convert.ToDecimal(txtValorBalanceo.Text) >= UmbarlVC)
            {
                optActivar.Enabled = false;
                optNoActivar.Enabled = false;
            }
            else
            {
                optActivar.Enabled = true;
                optNoActivar.Enabled = true;
            }

        }

        protected void chkRiesgo_CheckedChanged(object sender, EventArgs e)
        {

            foreach (GridViewRow row in gvRiesgos.Rows)
            {
                CheckBox check = row.FindControl("chkRiesgo") as CheckBox;
                DropDownList ddlProb = row.FindControl("ddlProbabilidad") as DropDownList;
                DropDownList ddlImpa = row.FindControl("ddlImpacto") as DropDownList;

                HiddenField hdd = row.FindControl("hddRiesgo") as HiddenField;

                if (check.Checked == true)
                {
                    //icalculo += (Convert.ToInt32(ddlProb.SelectedValue) * Convert.ToInt32(ddlImpa.SelectedValue));
                    dcalculo += (listaProbabilidad.Find(p => p.ID_PROBABILIDAD == (Convert.ToInt32(ddlProb.SelectedValue)))).VALOR * (listaImpacto.Find(p => p.ID_IMPACTO == (Convert.ToInt32(ddlImpa.SelectedValue)))).VALOR; 
                    //dcalculo += (listaProbabilidad.Find(p => p.ID_PROBABILIDAD == (Convert.ToInt32(ddlProb.SelectedValue)))).VALOR;

                }

            }

            txtValorBalanceo.Text = dcalculo.ToString();//icalculo.ToString();

            if (Convert.ToDecimal(txtValorBalanceo.Text) >= UmbarlVC)
            {
                optActivar.Enabled = false;
                optNoActivar.Enabled = false;
            }          
            else
            {
                optActivar.Enabled = true;
                optNoActivar.Enabled = true;
            }



        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            //Guarda componente
            string estado="";
            if (optActivar.Checked == true){
                estado="AC";
            }
            if (optNoActivar.Checked == true){
                estado="IN";
            }

            ComponenteControladora.ActualizarAprobacion(lblCodigo.Text, estado, txAObservacion.Text);

            //Guarda lista de riesgos
            RiesgoComponenteControladora.EliminaRiesgoComponente(lblCodigo.Text);

            foreach (GridViewRow row in gvRiesgos.Rows)
            {
                CheckBox check = row.FindControl("chkRiesgo") as CheckBox;
                DropDownList ddlProb = row.FindControl("ddlProbabilidad") as DropDownList;
                DropDownList ddlImpa = row.FindControl("ddlImpacto") as DropDownList;

                HiddenField hdd = row.FindControl("hddRiesgo") as HiddenField;

                if (check.Checked == true)
                {
                    RiesgoComponenteControladora.InsertaRiesgoComponente(Convert.ToInt32(hdd.Value), lblCodigo.Text, Convert.ToInt32(ddlProb.SelectedValue), Convert.ToInt32(ddlImpa.SelectedValue));
                }

            }

            MensajeJavascript("Información guardada con éxito.", "_Grabar");
        }




        public void MensajeJavascript(string _Mensaje, string _NombreFuncion)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('" + _Mensaje + "');");
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), _NombreFuncion, Convert.ToString(sb), true);
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Aprobar-Priorizar.aspx");
        }

           
    }
}